package com.jjc.android.util

import android.app.Activity
import android.view.View
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseViewHolder
import com.jjc.android.R
import com.jjc.android.UserManager
import com.jjc.android.base.BaseFragment
import com.lzy.okgo.request.DeleteRequest
import com.lzy.okgo.request.PostRequest
import com.lzy.okgo.request.PutRequest

fun <T> PostRequest<T>.paramEx(params:MutableMap<String,Any?>,isOntuotu:Boolean = false): PostRequest<T> {
    if(isOntuotu){
        params["applicationid"] = UserManager.applicationId()
    }
    return upJson(Convert.toJson(params))
}

fun <T> PostRequest<T>.paramEx(params:List<Any?>): PostRequest<T> {
    return upJson(Convert.toJson(params))
}

fun <T> PutRequest<T>.paramEx(params:Map<String,Any?>): PutRequest<T> {
    return upJson(Convert.toJson(params))
}

fun <T> DeleteRequest<T>.paramEx(params:Map<String,Any?>): DeleteRequest<T> {
    return upJson(Convert.toJson(params))
}

fun ImageView.showImage(url:String?,@DrawableRes placeholder:Int = R.drawable.shape_c1c8d7_10){
    Glide
        .with(this)
        .load(url)
        .placeholder(placeholder)
        .error(placeholder)
        .into(this)
}

fun BaseViewHolder.showImage(@IdRes viewId: Int, url:String?){
    getView<ImageView>(viewId).showImage(url)
}

fun <T : View> Activity.id(id: Int) = lazy {
    findViewById<T>(id)
}

fun <T : View> BaseFragment.id(id: Int) = lazy {
    this.rootView?.findViewById<T>(id)
}